Page({
  data: {
    goodsList: [], // 获取到的数组
    total: 0, // 数据的长度
    parms: {
      query: '',
      cid: '',
      pagenum: 1,
      pagesize: 10,
    },
    titleList: ['综合', '销量', '价格']
  },

  onLoad({ cid }) {
    const parms = { ...this.data.parms }
    parms.cid = cid || 906
    this.setData({ parms })
    this.getGoodsList()

  },

  //  获取数据请求
  getGoodsList() {
    wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search',
      data: this.data.parms,
      success: (res) => {
        const goodsList = [...this.data.goodsList, ...res.data.message.goods]
        const total = res.data.message.total
        this.setData({ total, goodsList })
      }
    })
  },

  // 上拉加载
  onReachBottom() {
    if (this.data.total <= this.data.goodsList.length) {
      wx.showToast({
        title: '没有更多了~',
        icon: "none"
      })
      return
    }

    const parms = {...this.data.parms}
    parms.pagenum++ 
    this.setData({parms})
    this.getGoodsList()
  },

  // 下拉刷新
  async onPullDownRefresh(){
    const goodsList = []
    const parms = {...this.data.parms}
    parms.pagenum = 1
    this.setData({goodsList,parms})
   await this.getGoodsList()
    wx.stopPullDownRefresh()
  }
})